Sprite Byte: Shooting

Level: Beginner - Intermediate

by Alyce Watson [http://alycesrestaurant.com/]

Home

A Contest!

Fast Bitmaps

Button Lesson

API Corner

Listbox Trivia

Listbox Demo 1

Listbox Demo 2

SpriteByte-Shooting

Qcard DLL-Lesson 1

DDOC Print Demo

Newsletter help

Index


Sprite Bytes are small tutorials that address a single method to be used with sprites in Liberty BASIC. This Sprite Byte is about shooting. There are three demo programs included with this issue: shoot.bas, shoot1.bas and shoot2.bas.

Why Shoot?

Many arcade games require some form of shooting, whether it is a space ship firing a missile, or a golfer striking a golf ball, or a basketball player shooting for the hoop.

Minimum Requirements

You'll need a user-controlled sprite or a game-controlled sprite that will do the shooting. You'll need a missile sprite of some sort. This can be a bomb, a baseball, or anything that can be launched or thrown. You'll also need a trigger that causes the missile to be fired. This can be a game-controlled event, or a user-controlled event, such as a keyboard press or mouse click.

The demos here will use a simple spaceship and missile. The ship will be controlled by the keyboard left and right arrow keys, and the missile will be fired in a straight line when the up arrow key is pressed.

Here is the spaceship and missile, just after the user has fired by pressing the up arrow key.

Sprite Movement and Background Scrolling

In the game, the spaceship will move left to right along the bottom of the window. The starry background will scroll, making it appear that the ship is traveling through space. Both of these techniques were covered in earlier Sprite Bytes, in issues #119 and #120.

Create a starry background with graphics commands, so you won't need to include a background bitmap. This routine creates 200 x,y coordinate pairs that are generated randomly, and at each one, it places a dot, or star, with the "set" command. It then uses "getbmp" to give the image an LB bitmap name, and uses that name with the "background" command to cause it to be the background for the game.

    'create a black background with stars:
    #1 "down; fill black; color white"
    for i = 1 to 200
        x = int(rnd(0)*maxX)
        y = int(rnd(0)*maxY)
        #1 "set ";x;" ";y
    next
    #1 "getbmp bkg 0 0 ";maxX;" ";maxY

    'set the background
    #1 "background bkg"

Managing the Missile Sprite

By default, sprites are located in the upper left corner of the graphicbox when the program begins. Position sprites with the "spritexy" command. Place the missile off the screen. It could simply be made invisible, but the game will probably require collision detection when it has developed further, and invisible sprites still trigger collisions. It is better to locate a missile offscreen when it is not active. Notice that when variables are used as part of a command, they are located outside the quotation marks, and blank spaces within the quotation marks are preserved.

    'set shot location off screen
    shotX=-100 : shotY=-100
    #1 "spritexy shot ";shotX;" ";shotY

When the missile is fired, it will be repositioned on the screen at the proper location. Whenever it is not active, it will be placed offscreen, as above.

Where Does the Missile Start?

The missile should appear to be fired from the ship. This doesn't look right, does it?

It should look like this:

To locate the missile properly, you must know the coordinates of the ship. These can be retrieved with the "spritexy?" command. You could simply start the missile at the same coordinates as the ship, but since it is a smaller image, it will be fired off to the side, instead of in the center.

In the demo programs, the ship is 40 pixels wide and the missile is 10 pixels wide. To be centered on the ship, it needs to be offset in the x direction by 15 pixels. This is determined by the following equation.

Offset = (ShipWidth-MissileWidth)/2
15     = (40 - 10)/2

So, the offset for this game is 15. To center the missile, find the X location of the ship and add 15 to it. The Y location is near the bottom of the ship, and that never changes in this game, so it is set equal to the variable maxY, which was assigned at the start of the game. MaxY is the lowest Y location on the game window.

    #1 "spritexy? ship x y"
    'start shot near center of ship
    shotX=x+15:shotY=maxY
    #1 "spritexy shot ";shotX;" ";shotY
    #1 "drawsprites"    'update screen!

Tracking the Missile

There are two ways to track the missile. The first way is to decrement the missile position for each frame of action, and when it reaches 0, remove it from action by locating it offscreen.

    shotY=shotY-5 'move shot up screen
    if shotY<=0 then
        shotX=-100
        shotY=-100
    end if

    #1 "spritexy shot ";shotX;" ";shotY

This method is used in the demo "shoot1.bas" that is included with this newsletter.

The second way to track the missile is to let Liberty BASIC do it for you. Do this with the "spritetravelxy" command. it causes the named sprite to travel to the x,y location specified, at the speed specified, and to trigger an event when it arrives, so program execution continues at the branch label specified.

#handle.ext "spritetravelxy SpriteName X Y speed [branchHandler]"

Here's the code for this method:

    #1 "spritexy? ship x y"
    'start shot near center of ship
    shotX=x+15:shotY=maxY
    'set shot to travel to top of screen and
    'fire event when it gets there
    #1 "spritexy shot ";shotX;" ";shotY
    #1 "spritetravelxy shot ";shotX;"  0 5 [checkShoot]"

Here is the handler that is called when the missile gets to the desired location. It places the missile offscreen to deactivate it.

[checkShoot]
    shotX=-100
    shotY=-100
 
   'move shot sprite off screen, update display
    #1 "spritexy shot ";shotX;" ";shotY
    #1 "drawsprites"    'update screen!
    goto [mainLoop]

This method is used in the demo "shoot2.bas" that is included with this newsletter.

Firing Again

If you do not monitor the status of the missile, it can be shot again any time the user presses the up arrow key. The program "shoot.bas" that is included with this newsletter shows this method. It is not ideal, though. When the user presses the up arrow key, the missile disappears from its previous location and is fired again.

You can stop the user from firing again until the missile has reached the top of the screen. Do this by setting a flag to designate the status of the missile. This one is called "SHOOTING." It is set to 0 when the missile is not active, and one when it is active.

SHOOTING = 1   'missile is active

In the routine that handles the event of the user pressing the up arrow key, check to see if a missile is active by checking the value of the "SHOOTING" variable. If it is set to "1" then don't allow the user to shoot again.

[goShoot]
    'if SHOOTING flag is set, don't shoot again
    if SHOOTING=1 then return
    #1 "spritexy? ship x y"
    'start shot near center of ship
    shotX=x+15:shotY=maxY
    SHOOTING=1
    return

Don't forget to set the flag to 1 when the missile is activated, and back to 0 when it is removed from the screen!

[goShoot]
    'if SHOOTING flag is set, don't shoot again
    if SHOOTING=0 then
        #1 "spritexy? ship x y"
        'start shot near center of ship
        shotX=x+15:shotY=maxY
        SHOOTING=1
        'set shot to travel to top of screen and
        'fire event when it gets there
        #1 "spritexy shot ";shotX;" ";shotY
        #1 "spritetravelxy shot ";shotX;"  0 5 [checkShoot]"
    end if
    return


[checkShoot]
    shotX=-100
    shotY=-100
    SHOOTING=0  'reset shooting flag
    'move shot sprite off screen, update display
    #1 "spritexy shot ";shotX;" ";shotY
    #1 "drawsprites"    'update screen!
    goto [mainLoop]

More About Shooting!

Future Sprite Bytes will deal with allowing multiple missiles on the screen at once time, shooting by game-controlled sprites such as enemy ships, and collision detection. Collision detection discovers whether the missile has hit anything.


Home

A Contest!

Fast Bitmaps

Button Lesson

API Corner

Listbox Trivia

Listbox Demo 1

Listbox Demo 2

SpriteByte-Shooting

Qcard DLL-Lesson 1

DDOC Print Demo

Newsletter help

Index